package com.study.animationstudy.ui

import android.animation.ObjectAnimator
import android.animation.RectEvaluator
import android.graphics.Rect
import android.view.View
import android.widget.AdapterView
import android.widget.AdapterView.OnItemSelectedListener
import android.widget.ArrayAdapter
import com.study.animationstudy.R
import com.study.animationstudy.base.BaseActivity
import com.study.animationstudy.databinding.ActivityObjectAnimBinding

class ObjectAnimActivity : BaseActivity<ActivityObjectAnimBinding>() {
    // 声明四个属性动画对象
    private lateinit var alphaAnim:ObjectAnimator
    private lateinit var translateAnim:ObjectAnimator
    private lateinit var scaleAnim:ObjectAnimator
    private lateinit var rotateAnim:ObjectAnimator
    private val objectArray = arrayOf("灰度动画", "平移动画", "缩放动画", "旋转动画", "裁剪动画")

    override fun ActivityObjectAnimBinding.initBinding() {
        initObjectAnim()
        initObjectSpinner()
    }

    /**
     * 初始化属性动画
     */
    private fun initObjectAnim(){
        // 构造一个在透明度上变化的属性动画
        alphaAnim = ObjectAnimator.ofFloat(mBinding.ivObjectAnim,"alpha",1f,0.1f,1f)
        // 构造一个在横轴上平移的属性动画
        translateAnim = ObjectAnimator.ofFloat(mBinding.ivObjectAnim,"translationX",0f,-200f,0f,200f,0f)
        // 构造一个在纵轴上缩放的属性动画
        scaleAnim = ObjectAnimator.ofFloat(mBinding.ivObjectAnim,"scaleY",1f,0.5f,1f)
        // 构造一个围绕中心点旋转的属性动画
        rotateAnim = ObjectAnimator.ofFloat(mBinding.ivObjectAnim,"rotation",0f,360f,0f)
    }

    private fun initObjectSpinner(){
        val objectAdapter = ArrayAdapter<String>(this,R.layout.item_select,objectArray)
        mBinding.spObject.apply {
            prompt = "请选择属性动画类型"
            adapter = objectAdapter
            onItemSelectedListener = object: OnItemSelectedListener{
                override fun onItemSelected(
                    parent: AdapterView<*>?,
                    view: View?,
                    position: Int,
                    id: Long
                ) {
                    playObjectAnim(position)
                }

                override fun onNothingSelected(parent: AdapterView<*>?) {

                }

            }
        }
    }

    /**
     * 播放指定类型的属性动画
     * @param type Int
     */
    private fun playObjectAnim(type:Int){
        var anim:ObjectAnimator? = null
        when(type){
            // 灰度动画
            0 -> anim = alphaAnim
            // 平移动画
            1 -> anim = translateAnim
            // 缩放动画
            2 -> anim = scaleAnim
            // 旋转动画
            3 -> anim = rotateAnim
            // 裁剪动画
            4 -> {
                val width = mBinding.ivObjectAnim.width
                val height = mBinding.ivObjectAnim.height
                // 构造一个从四周向中间裁剪的属性动画
                val clipAnim = ObjectAnimator.ofObject(mBinding.ivObjectAnim,"clipBounds",
                    RectEvaluator(), Rect(0, 0, width, height),
                    Rect(width / 3, height / 3, width / 3 * 2, height / 3 * 2),
                    Rect(0, 0, width, height))
                anim = clipAnim
            }
        }
        anim?.apply {
            // 设置动画的播放时长
            duration = 3000
            // 开始播放属性动画
            start()
        }
    }

}